//
//  LessonViewController.m
//  LoveDraw
//
//  Created by 梁晨 on 2017/6/13.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "LessonViewController.h"
#import "LessonModel.h"
#import "LessonTableViewCell.h"
#import "LessonDetailViewController.h"

#import "MessageListViewController.h"
#import "RecordViewController.h"
#import "TimetableViewController.h"
#import "LessonShareViewController.h"




@interface LessonViewController ()<UITableViewDelegate,UITableViewDataSource,LessonDelegate>
{
    int page;
}
@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;
@end


@implementation LessonViewController

-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(UITableView *)listV
{
    if (!_listV) {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _listV.backgroundColor = [UIColor whiteColor];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        [_listV registerNib:[UINib nibWithNibName:@"LessonTableViewCell" bundle:nil] forCellReuseIdentifier:@"LessonTableViewCell"];
        _listV.separatorStyle = 0;
        _listV.estimatedRowHeight = 150;
    }
    return _listV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listV];
    [self Request];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"我的课程";
}


#pragma mark ---tableV代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //
    LessonModel *model = self.listArr[indexPath.row];
    LessonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LessonTableViewCell" forIndexPath:indexPath];
    [cell.titleimageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,model.catethumb]] placeholderImage:Kplaceholderimage];
    cell.Delegate = self;
    cell.ClassL.text = model.coursename;
    cell.TeacherL.text = [NSString stringWithFormat:@"任课老师%@",model.teachername];

    cell.begainTimeL.text = [NSString stringWithFormat:@"有效期%@--%@",model.joindate,model.enddate];
    
    cell.NumberOfClassL.text = [NSString stringWithFormat:@"已经使用%ld课次 剩余%@课次",[model.classhour_use integerValue],model.classhour_last];
    

    cell.selectionStyle = 0;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

/*@property(nonatomic,strong)NSString *catethumb;
 @property(nonatomic,strong)NSString *teachername;
 @property(nonatomic,strong)NSString *coursename;*/

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      LessonModel *model = self.listArr[indexPath.row];
    LessonDetailViewController *vc = [[LessonDetailViewController alloc]init];
    vc.sid = model.aid;
    vc.cid = model.cid;
    vc.aid = model.aid;
    vc.catethumb = model.catethumb;
    vc.teachername = model.teachername;
    vc.coursename = model.coursename;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)BtnClick:(UIButton *)btn index:(NSInteger )index
{
    LessonTableViewCell *cell = (LessonTableViewCell *)btn.superview.superview;
    NSIndexPath *indexpath = [self.listV indexPathForCell:cell];
    if (index == 0 ) {
        [self LiuyanAct:indexpath];
    }else if (index == 1)
    {
        [self KeBiaoAct:indexpath];
    }else if (index == 2)
    {
        [self KeLiAct:indexpath];
    }else
    {
        [self DianMing:indexpath];
    }
}

// 留言
-(void)LiuyanAct:(NSIndexPath *)indexpath
{
    LessonModel *model = self.listArr[indexpath.row];
    MessageListViewController *list = [[MessageListViewController alloc]init];
    list.caid = model.aid;
    [self.navigationController pushViewController:list animated:YES];
}

// 课表
-(void)KeBiaoAct:(NSIndexPath *)indexpath
{
    LessonModel *model = self.listArr[indexpath.row];
    TimetableViewController *VC = [[TimetableViewController alloc]init];
    VC.cid = model.cid;
    VC.sid = model.sid;
    VC.aid = model.aid;
    [self.navigationController pushViewController:VC animated:YES];
}



// 课例
-(void)KeLiAct:(NSIndexPath *)indexpath
{
     LessonModel *model = self.listArr[indexpath.row];
    LessonShareViewController *VC = [[LessonShareViewController alloc]init];
    VC.cid = model.cid;

    [self.navigationController pushViewController:VC animated:YES];
}

// 点名
-(void)DianMing:(NSIndexPath *)indexpath
{
     LessonModel *model = self.listArr[indexpath.row];
    RecordViewController *vc = [[RecordViewController alloc]init];
    vc.aid = model.aid;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark--- 网络请求部分
-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCourse&a=index" parmDic:@{@"uid":[[NSUserDefaults standardUserDefaults]objectForKey:kUserId],@"access_token":[[NSUserDefaults standardUserDefaults]objectForKey:kAccessToken]} requestType:RequestGET Success:^(id data)
    {
        NSArray *dataArr = data;
        self.listArr = [LessonModel modelCongfigure:dataArr];

        [self.listV reloadData];
    } error:^(NSError *error) {

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
